var apps, views, user, sendData;  //sendData 为要发送的数据集
var noUiSlider_obj;
	mui.plusReady(function () {
	   	user = bexta.isLogin(true);
	   	views = bexta.getWebview();
	   	sendData = views.sendData || {};
	 	user && user.uid && (sendData['uid'] = user.uid);
	 	sendData['firstrow'] = 0;
	 	sendData['listrow'] = 10;
	 	sendData['commend_id'] = views.type || 1;  //必须要有一个commend_id,以防报错
	 	if( views.sid ){
	 		sendData['sid'] = views.sid;
	 	}
//	 	bexta.jsonToString(se/ndData)
		bexta.ajax(api.new_search_goods, function(res){
			initVue(res);
		}, {data:sendData});
	});
	function getData(cfun, first, last) {
		apps.send['firstrow'] = first;
		apps.send['listrow'] = last
//		sendData['sid'] = views.sid;
//bexta.jsonToString(apps.send)
		bexta.ajax(api.new_search_goods, function(res){
			cfun(res);
		}, {data:apps.send});
	}
//高级筛选, 回调,更改查询数据!
window.addEventListener('toggleData', function(evt){
	apps.send = evt.detail.sendData;
	apps.send['commend_id'] = evt.detail.sendData.type;
	if( views.sid ){
 		apps.send['sid'] = views.sid;
 	}
	apps.attr = evt.detail.sendData.attr_id.split(',');
	apps.class = evt.detail.sendData.class_id || [];
	apps.city = evt.detail.sendData.city_id || [];
	apps.send['firstrow'] = 0;
	apps.send['listrow'] = 10;
	getData(function(res){
		apps.list.list = res.list; 
		apps.first = res.list.length; 
		mui('#refreshContainer').pullRefresh().refresh(true);  //重置上拉
		mui.later(function(){
			mui('#refreshContainer').pullRefresh().scrollTo(0,0,100)
		},10);
	},0, apps.last);
});
	function initVue(res){
		apps = new Vue({
			el:"#vueMain",
			data:{
				look:views.look || '',
				list:res,
				first:sendData.firstrow,
				last:sendData.listrow,
				attr:[],   //属性
				class:[],  //类型
				city:[],  //城市 
				model:0,  //套餐
				startPrice:0,
				endPrice:0,
				send:sendData
			},
			mounted:function(){
				document.querySelector('.loading').classList.add('loadRun');
				var self = this;
				mui('.select-list').on('tap', '.mui-checkbox', function(evt){
	    			})
				if( document.getElementById('noUiSlider') ){
					noUiSlider_obj = document.getElementById('noUiSlider');
					noUiSlider.create(noUiSlider_obj, {
						start: [1, 2500],
						connect: true,
						setp: 10,
						range: {
							min: [1],
							max: [5000]
						}
					});
					noUiSlider_obj.noUiSlider.on('update', function(val, index) {
						var span = document.querySelectorAll('.nouder .mui-clearfix span');
						for(var i = 0; i < val.length; i++) {
							if(val[i] > 999) {
								val[i] = (parseFloat(val[i]) / 1000).toFixed(2) + 'K';
							} else {
								val[i] = parseInt(val[i]);
							}
						}
						span[index].innerText = val[index];
					});
				}
				
				self.first = self.list.list.length; //记录一下获取多少条记录
				mui.init();
				mui('#refreshContainer').scroll();
				mui('.select-list .mui-scroll-wrapper').scroll();
				mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
						mui(pullRefreshEl).pullToRefresh({
							up: {
								callback: function() {
									var that = this;
									getData(function(res){
							      		if( res.list.length < self.last ){
							      			 that.endPullUpToRefresh(true);
							      		}else{
							      			that.endPullUpToRefresh(false);
							      		}
							      		mui.each(res.list, function(){
						      				self.list.list.push( this );
						      			});
						      			self.first = self.list.list.length;
							      	}, self.first, self.last);
								}
							}
						});
					});
				initEvent();
				initMounted();
				bexta.imageLoad('[data-load]');
			},
			updated:function(){
				//星星初始化
			   	mui('.score_star').score({
					eventstar:false, 
					fullStar:"../icon/icon_xingxing@2x.png",
					halfStar:"../icon/icon_bankexin@2x.png",
					voidStar:"../icon/icon_void@2x.png"
				});
				bexta.imageLoad('[data-load]');
			}
		});
	}
	function initMounted() { 
		//单选
		mui('#spec').on('tap', '.mui-checkbox', function(){
			mui('#spec input:checked').each(function(){
				this.checked = false;
			});
			this.querySelector('input').checked = true;
			return false;
		});
		//收藏
		mui('body').on('tap', '.collect', function(){
			var user = bexta.isLogin(true);
			if( !user ){
				//未登录
				mui.confirm("您还没有登录,是否登录?", TITLE, CONFIRM, function(item){
					if( item.index == 1 ){
						mui.openWindow({
							id:'login',
							url: '../center/login.html',
							show:{autoShow:true,aniShow:'zoom-fade-out'},
							waiting:{autoShow:false},
							extras:{
								refresh:'refresh'
							}
						});
					}
				});
			}else{
				//已经登录
				var idx = this.getAttribute('data-idx'),
					oid = this.getAttribute('data-id');
				if( apps.list.list[idx].id == oid ){
					var list = apps.list.list[idx];
					if( list.is_collect == 0 ){
						//加
//						bexta.jsonToString(list)
						bexta.ajax(api.collect, function(res,msg){
							mui.toast(msg);
							apps.list.list[idx].is_collect = 1;
						}, {data:{uid:user.uid, goods_id:list.id}});
					}else{
						//删除
						bexta.ajax(api.del_collect, function(res,msg){
							mui.toast(msg);
							apps.list.list[idx].is_collect = 0;
						},{ data:{uid:user.uid,goods_id:list.id, product_id:0 } });
					}
				}else{
					mui.toast('收藏失败，数据有误');
				}
			}
		});
		//->收藏 end
		
		/**
		 * 重置或添加属性后,调用数据
		 */
		function checkData(){
			document.querySelector('.select-list.run').classList.remove('run');
	   		document.querySelector('.type.run').classList.remove('run');
	   		document.querySelector('.mark').style.display = 'none';
			apps.send['attr_id'] = apps.attr.length!=0 && apps.attr.join(',') ||"";
			apps.send['city_id'] = apps.city.length!=0 && apps.city.join('_') || "";
			apps.send['class_id'] = apps.class.length && apps.class.join('_') || "";
			apps.send['model_id'] = apps.model;
			apps.send['end_price'] = apps.endPrice;
			apps.send['start_price'] = apps.startPrice;
			getData(function(res){
				apps.list.list = res.list; 
				apps.first = res.list.length;  
				mui('.mui-slider-group .mui-scroll').pullToRefresh().refresh(true);  //重置上拉
				mui.later(function(){
//					mui.scrollTo(0,0) 
					mui('.mui-slider-group .mui-scroll-wrapper').scroll().scrollTo(0,0,100)
//					mui('.mui-slider-group .mui-scroll').pullToRefresh().scrollTo(0,0,100)
				},10);
			},0, apps.last);
		}
		
		//添加属性,分类,城市 
		mui('#vueMain').on('tap', '.select-btn-submit', function(){
			if(  this.classList.contains('attr') ){
				//属性
				var id = this.getAttribute('data-id');
				var oli = document.querySelectorAll('#attr'+id+" input:checked");
				var arr = []
				mui.each(apps.attr, function(){
					if( this.indexOf(id+"*") == -1 ){
						arr.push( this );
					}
				});
				apps.attr = arr;
				mui.each(oli, function(){
					apps.attr.push(id+"*"+this.value);
				});
			}
			
			if( this.classList.contains('sequ') ){
				//分类
				var sequ = document.querySelectorAll('#sequ input:checked');
				apps.class = [];
				mui.each( sequ , function(){
					apps.class.push( this.value );
				});
			}
			if( this.classList.contains('scity') ){
				//城市 
				var citys = document.querySelectorAll('#scity input:checked');
				apps.city = [];
				mui.each(citys, function(){
					apps.city.push(this.value);
				});
			}
			if( this.classList.contains('spec') ){
				//套餐
				var ck = document.querySelector('#spec input:checked');
				apps.model = ck.value;
			}
			if( this.classList.contains('price') ){
				//价格
				var v = noUiSlider_obj.noUiSlider.get();
				apps.startPrice = v[0];
				apps.endPrice = v[1];
			}
			checkData();
		});
		
		//重置属性,分类,城市 
		mui('#vueMain').on('tap', '.select-btn-reset', function(){
			if( this.classList.contains('attr') ){
				var id = this.getAttribute('data-id');
				var oli = document.querySelectorAll('#attr'+id+" input:checked");
				var arr = [];
				mui.each(apps.attr, function(){
					if( this.indexOf(id+"*") == -1 ){
						arr.push(this);
					}
				})
				apps.attr = arr;
				mui.each(oli, function(){
					this.checked = false;
				});
			}
			if( this.classList.contains('sequ') ){
				var sequ = document.querySelectorAll('#sequ input:checked');
				mui.each(sequ, function(){
					this.checked = false;
				})
				apps.class = [];
			}
			if( this.classList.contains('scity') ){
				var citys = document.querySelectorAll('#scity input:checked');
				mui.each(citys, function(){
					this.checked = false;
				})
				apps.city = [];
			}
			if( this.classList.contains('spec') ){
				var oli = document.querySelectorAll('#spec'+id+" input:checked");
				mui.each(oli, function(){
					this.checked = false;
				});
				apps.model = 0;
			}
			if( this.classList.contains('price') ){
				//价格
				noUiSlider_obj.noUiSlider.set( [1,2500] );
				apps.startPrice = 0;
				apps.endPrice = 2500;
			}
			checkData();
		});
	}
	
	
	
	function initEvent(){
		/*
	   	 * 根据要求,有一个最大的高度,不超过最大的高度,则使用原始高度
	   	 */
	   	var windowHeight = parseInt(window.innerHeight*0.6); //最大高度
	   	mui('.select-list').each(function(index, ele){
	   		//要先计算出里面mui-scroll的高度
	   		var boxHeight = this.querySelector('.mui-scroll').clientHeight;
	   		var transformHeight = 0; //运作 动画的高度
	   		//判断是否大于最大高度
	   		if( boxHeight > windowHeight ){
	   			//大于,那么就用最大的高度作为高度
	   			transformHeight = windowHeight;
	   			this.style.height = (windowHeight+50) +'px';
	   		}else{
	   			//小于最大高度,那么直接等于这于这个高度
	   			transformHeight = boxHeight;
	   			this.style.height = (boxHeight + 50) + 'px';
	   		}
	   		this.style.top = - transformHeight + 'px';
	   	});
	   	
	   	mui('body').on('tap', '.type', function(){
	   		var tab = this.getAttribute('data-tab');
	   		if( !tab ) return ;
	   		var tabobj = document.querySelector('#'+tab);
	   		if( tabobj.classList.contains('run') ){
	   				this.classList.remove('run');
	   				tabobj.classList.remove('run');
	   				document.querySelector('.mark').style.display = 'none';
	   			}else{
	   				mui('.type').each(function(){
	   					this.classList.remove('run');
	   				});
	   				//要清除所有运行中的动画
	   				mui('.select-list').each(function(){
	   					this.classList.remove('run');
	   				});
	   				this.classList.add('run');
	   				document.querySelector('.mark').style.display = 'block';
	   				mui.later(function(){
	   					this.classList.add('run');
	   				},10, tabobj )
	   				
	   			}
	   	});
	   	document.querySelector('.mark').addEventListener('tap', function(){
	   		//点击模态背景 删除所有动画
	   		document.querySelector('.select-list.run').classList.remove('run');
	   		document.querySelector('.type.run').classList.remove('run');
	   		this.style.display = 'none';
	   	});
	   	//星星初始化
	   	mui('.score_star').score({
			eventstar:false, 
			fullStar:"../icon/icon_xingxing@2x.png",
			halfStar:"../icon/icon_bankexin@2x.png",
			voidStar:"../icon/icon_void@2x.png"
		});
	}